using System;
using System.IO;
using Bookvar.ObjectModel;

namespace Bookvar.ServiceModel.Actions
{
    public class SaveFileResourceToFileAction : ResourcePersistenceAction
    {
        private readonly string fileName;

        public override bool CanUndo
        {
            get { return false; }
        }

        public override bool CanRedo
        {
            get { return false; }
        }

        public SaveFileResourceToFileAction( IPersistenceService persistenceService, MindMap mindmap,
                                             IFileResource resource, string fileName )
            : base( persistenceService, mindmap, resource )
        {
            this.fileName = fileName;
        }

        public override void Execute()
        {
            using ( var saveAsFileStream = new FileStream( fileName, FileMode.Create, FileAccess.ReadWrite ) )
            {
                CopyResourceStream( saveAsFileStream );
            }
        }

        public override void Undo()
        {
            throw new InvalidOperationException();
        }
    }
}